﻿using System;
using System.Collections.ObjectModel;
using System.Xml.Serialization;

namespace WhiteFlow.DevTrackingTool.UI.Models
{
    [Serializable]
    public class TrackingDaySummary : WorksheetItem
    {
        #region Constants

        public const string AllTasksTotalTimePropertyName = "AllTasksTotalTime";

        public const string AllTasksTotalTimeDecimalPropertyName = "AllTasksTotalTimeDecimal";

        public const string OvertimePropertyName = "Overtime";

        public const string OvertimeDecimalPropertyName = "OvertimeDecimal";

        public const string TasksSummaryPropertyName = "TasksSummary";

        #endregion

        #region Fields

        private TimeSpan allTasksTotalTime;

        private double allTasksTotalTimeDecimal;

        private TimeSpan overtime;

        private double overtimeDecimal;

        private ObservableCollection<TaskExecutionSummary> tasksSummary;

        #endregion

        #region Properties

        [XmlIgnore]
        public TimeSpan AllTasksTotalTime
        {
            get { return allTasksTotalTime; }
            set
            {
                allTasksTotalTime = value;
                OnPropertyChanged(AllTasksTotalTimePropertyName);
                OnWorksheetItemChanged();

                AllTasksTotalTimeDecimal = allTasksTotalTime.TotalHours;
            }
        }

        [XmlElement(ElementName = AllTasksTotalTimePropertyName)]
        public long AllTasksTotalTimeTicks
        {
            get { return AllTasksTotalTime.Ticks; }
            set { AllTasksTotalTime = new TimeSpan(value); }
        }

        [XmlIgnore]
        public double AllTasksTotalTimeDecimal
        {
            get { return allTasksTotalTimeDecimal; }
            set
            {
                allTasksTotalTimeDecimal = Math.Round(value, 2, MidpointRounding.AwayFromZero);
                OnPropertyChanged(AllTasksTotalTimeDecimalPropertyName);
                OnWorksheetItemChanged();
            }
        }

        [XmlIgnore]
        public TimeSpan Overtime
        {
            get { return overtime; }
            set
            {
                overtime = value;
                OnPropertyChanged(OvertimePropertyName);
                OnWorksheetItemChanged();

                OvertimeDecimal = overtime.TotalHours;
            }
        }

        [XmlElement(ElementName = OvertimePropertyName)]
        public long OverimeTicks
        {
            get { return Overtime.Ticks; }
            set { Overtime = new TimeSpan(value); }
        }

        [XmlIgnore]
        public double OvertimeDecimal
        {
            get { return overtimeDecimal; }
            set
            {
                overtimeDecimal = Math.Round(value, 2, MidpointRounding.AwayFromZero);
                OnPropertyChanged(OvertimeDecimalPropertyName);
                OnWorksheetItemChanged();
            }
        }

        [XmlArray(ElementName = TasksSummaryPropertyName, IsNullable = true)]
        [XmlArrayItem(ElementName = "TaskExecutionSummary", IsNullable = false, Type = typeof(TaskExecutionSummary))]
        public ObservableCollection<TaskExecutionSummary> TasksSummary
        {
            get { return tasksSummary; }
            set
            {
                tasksSummary = value;
                OnPropertyChanged(TasksSummaryPropertyName);
            }
        }

        #endregion
    }
}